﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodonSegDP;
using AlgorithmResult;
using PermutationCl;

namespace TripletSegmentationGUI
{
    public partial class BoundaryPositionTabletable : UserControl
    {
        public enum NUCLEOTID { A = 0, T = 1, C = 2, G = 3 };
        int codonSize = 3;
        int numRows = 0;
        Dictionary<String, int> codonMap = new Dictionary<string, int>();

        public Dictionary<String, int> CodonMap
        {
            get { return codonMap; }
           
        }
        private ComboBox combobox1 = new ComboBox();
        public DataTable table = null;
        DataGridViewComboBoxColumn comboboxColumn;
        public BoundaryPositionTabletable(DataTable _table)
        {
            InitializeComponent();
            try
            {
                table = _table;
                this.dataGridViewTable.DataSource = table;
                combobox1.Hide();

                this.dataGridViewTable.Controls.Add(combobox1);


                /*
                dataGridViewTable.NewRowNeeded +=
                new DataGridViewRowEventHandler(dataGridView1_NewRowNeeded);
                dataGridViewTable.RowsAdded +=
                    new DataGridViewRowsAddedEventHandler(dataGridView1_RowsAdded);
                dataGridViewTable.CellValidating +=
                    new DataGridViewCellValidatingEventHandler(dataGridView1_CellValidating);
                dataGridViewTable.CellValueNeeded +=
                    new DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);
                dataGridViewTable.CellValuePushed +=
                    new DataGridViewCellValueEventHandler(dataGridView1_CellValuePushed);

                numRows = table.Rows.Count;

                dataGridViewTable.VirtualMode = true;
                dataGridViewTable.AllowUserToDeleteRows = false;
                for (int i = 0; i < table.Columns.Count; i++)
                    dataGridViewTable.Columns.Add(table.Columns[i].ColumnName, table.Columns[i].ColumnName);
    
                dataGridViewTable.Columns.Remove(table.Columns[DataTableResult.STR_BoundaryIndexes].ToString());
                AddComboBoxColumns();
                ////   dataGridViewTable.Rows.AddCopies(0, table.Rows.Count);
                   this.dataGridViewTable.RowCount = numRows;
                 */
             
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        bool newRowNeeded;
        private void AddComboBoxColumns()
        {

            try
            {
                comboboxColumn = CreateComboBoxColumn();
                SetAlternateChoicesUsingDataSource(comboboxColumn);
                comboboxColumn.HeaderText = DataTableResult.STR_BoundaryIndexes;
                dataGridViewTable.Columns.Insert(table.Columns.Count-1, comboboxColumn);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            /*
            comboboxColumn = CreateComboBoxColumn();
            SetAlternateChoicesUsingItems(comboboxColumn);
            comboboxColumn.HeaderText = "TitleOfCourtesy (via Items property)";
            // Tack this example column onto the end.
            dataGridViewTable.Columns.Add(comboboxColumn);
             * */
        }
        private DataGridViewComboBoxColumn CreateComboBoxColumn()
        {
            DataGridViewComboBoxColumn column =
                new DataGridViewComboBoxColumn();
            {
                column.DataPropertyName = table.Columns[DataTableResult.STR_BoundaryIndexes].ToString();
                column.HeaderText = table.Columns[DataTableResult.STR_BoundaryIndexes].ToString();
                column.DropDownWidth = 160;
                column.Width = 90;
                column.MaxDropDownItems = 10;
                column.FlatStyle = FlatStyle.Standard;
                column.ReadOnly = false;
                
            }
            return column;
        }
        private void SetAlternateChoicesUsingDataSource(DataGridViewComboBoxColumn comboboxColumn)
        {
            {

                
                BindingList<List<String>> list = new BindingList<List<String>>();
                char[] sepator = new char[4];
                sepator[0] = '\n';
                sepator[1] = ',';
                sepator[2] = ' ';
                sepator[3] = '\r';
                
               
                for(int i=0;i<table.Rows.Count;++i)
                {
                    String BoundaryString =(String)table.Rows[i][DataTableResult.STR_BoundaryIndexes];
                    String[] boundaries = BoundaryString.Split(sepator);
                    list.Add(boundaries.ToList());
                    comboboxColumn.Items.AddRange(boundaries);
                }

             //   comboboxColumn.Items.AddRange(new obJect[] { "A", "B" });
               
                //comboboxColumn.DataSource = table;//.Columns[DataTableResult.STR_BoundaryIndexes].Container;
                //comboboxColumn.ValueMember =DataTableResult.STR_BoundaryIndexes;
                //comboboxColumn.DisplayMember = comboboxColumn.ValueMember;
               
               
            }
        }


        private void dataGridView1_NewRowNeeded(object sender,
            DataGridViewRowEventArgs e)
        {
            newRowNeeded = true;
        }
        private void dataGridView1_RowsAdded(object sender,
       DataGridViewRowsAddedEventArgs e)
        {
            if (newRowNeeded)
            {
                newRowNeeded = false;
                numRows = numRows + 1;
            }
        }
        bool match(String str) {
            if (str == "")
                return true;
            return false;
        } 

        const int initialValue = -1;
        private void dataGridView1_CellValueNeeded(object sender,
        DataGridViewCellValueEventArgs e)
        {
            if ((e.RowIndex)<table.Rows.Count)
            {
                if (e.ColumnIndex == 8) {
                    char[] sepator = new char[5];
                    sepator[0] = '\n';
                    sepator[1] = ',';
                    sepator[2] = ' ';
                    sepator[3] = '\r';
                    sepator[4] = '\0';


                 
                        String BoundaryString = (String)table.Rows[e.RowIndex][DataTableResult.STR_BoundaryIndexes];
                        String[] boundaries = BoundaryString.Split(sepator);
                        List<String> list = boundaries.ToList();

                        list.RemoveAll(match);
                        //for (int i = 0; i < list.Length; ++i)
                        //{
                        //    if(boundaries[i]!="")
                  //      e.Value = list;
                        ((DataGridViewComboBoxCell)dataGridViewTable.Rows[e.RowIndex].Cells[8]).Items.AddRange(list);
                        //}
                }else
                
                // Use the store if the e value has been modified 
                // and stored.            
                e.Value = table.Rows[e.RowIndex][e.ColumnIndex];
            }
            else if (newRowNeeded && e.RowIndex == table.Rows.Count)
            {
                if (dataGridViewTable.IsCurrentCellInEditMode)
                {
                    e.Value = initialValue;
                }
                else
                {
                    // Show a blank value if the cursor is just resting
                    // on the last row.
                    e.Value = String.Empty;
                }
            }
            else
            {
                e.Value = e.RowIndex;
            }
        }
        private void dataGridView1_CellValidating(object sender,
       DataGridViewCellValidatingEventArgs e)
        {
 
        }
        private void dataGridView1_CellValuePushed(object sender,
       DataGridViewCellValueEventArgs e)
        { }
        private void dataGridViewTable_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
           
           /* * */
            if (e.ColumnIndex == 9)
            {
                this.combobox1.Location = this.dataGridViewTable.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;
                this.combobox1.Width = this.dataGridViewTable.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Width;
                //     combobox1.Dock = DockStyle.Fill;
                this.combobox1.Items.Clear();
                char[] sepator = new char[4];
                sepator[0] = '\n';
                sepator[1] = ',';
                sepator[2] = ' ';
                sepator[3] = '\r';
                String BoundaryString = this.dataGridViewTable.SelectedCells[0].Value.ToString();
                String[] boundaries = BoundaryString.Split(sepator);
                foreach (String boundary in boundaries)
                {
                    if (boundary != "")
                        this.combobox1.Items.Add(boundary);
                }
                this.combobox1.SelectedIndex = 0;


                this.combobox1.Show();
                this.combobox1.DroppedDown = true;
            }
            
          
        }

        private void dataGridViewTable_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            this.combobox1.Hide();
        }

        private void dataGridViewTable_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {

            //if (e.ColumnIndex == 8 && e.RowIndex!=-1)
            //{
            //    DataGridView dataGridView= sender as DataGridView;
            //    DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)dataGridView.Columns[8];
            //    char[] sepator = new char[4];
            //    sepator[0] = '\n';
            //    sepator[1] = ',';
            //    sepator[2] = ' ';
            //    sepator[3] = '\r';
            //    // table.Columns[DataTableResult.STR_BoundaryIndexes].Spl;
            //    col.Items.AddRange((table.Rows[e.RowIndex][e.ColumnIndex].ToString()).Split(sepator));
            //}
        }

   
    }
}
